use async_std::net::TcpListener;
use async_std::task::spawn;
use futures::stream::StreamExt;
use webserver::handle_connection;

#[async_std::main]
async fn main() -> async_std::io::Result<()> {
    let listener = TcpListener::bind("127.0.0.1:7878").await?;

    listener
        .incoming()
        .for_each_concurrent(None, |stream_res| async move {
            match stream_res {
                Ok(stream) => {
                    spawn(async move {
                        handle_connection(stream).await;
                    });
                }
                Err(err) => eprintln!("failed to accept connection: {err}"),
            }
        })
        .await;

    Ok(())
}
